package com.budwk.app.sqfwV2.component.centuryCity;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwV2.dto.dto.SafeCheckTaskPdfDTO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class CenturyCitySecurityComponent {

    public static Sqfw_formSummary_new queryLastData(Sqfw_formSummary_new summaryNew, String type) {
        SqfwFormsummaryNewService formsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        SafeCheckTaskPdfDTO roadPdfDTO = JSONObject.parseObject(summaryNew.getParamData(), SafeCheckTaskPdfDTO.class);
        List<Sqfw_formSummary_new> formSummaryNews = formsummaryNewService.queryByPlaceAndAddress(roadPdfDTO.getPlaceName(),
                roadPdfDTO.getAddress(), type, summaryNew.getUnitId());
        if (formSummaryNews.size() == 0) {
            return null;
        }
        // 取出小于当前提交时间的所有数据
        List<Sqfw_formSummary_new> formSummaryData = formSummaryNews.stream().filter(e -> e.getCreateDate().getTime() < summaryNew.getCreateDate().getTime()).collect(Collectors.toList());
        if (formSummaryData.size() > 0) {
            return formSummaryData.get(0);
        }
        return null;
    }

    /**
     * 按照商铺名称和地址获取最新的数据
     * @param summaryList
     * @return
     */
    public static List<Sqfw_formSummary_new> handleDatas(List<Sqfw_formSummary_new> summaryList) {
        List<Sqfw_formSummary_new> summaryAllList = summaryList.stream().sorted(Comparator.comparing(Sqfw_formSummary_new::getCreateDate).reversed())
                .collect(Collectors.toList());
        List<Sqfw_formSummary_new> list = new ArrayList<>();
        // 按照商铺名称和地址去重得到所有数据
        Set<String> set = new HashSet<>();
        for (Sqfw_formSummary_new summaryNew : summaryAllList) {
            JSONObject json = JSONObject.parseObject(summaryNew.getParamData());
            String param = json.getString("placeName") + json.getString("address");
            if (!set.contains(param)) {
                set.add(param);
                list.add(summaryNew);
            }
        }
        return list;
    }

    public static void buildNum(List<Map<String, Object>> list) {
        AtomicInteger num = new AtomicInteger(1);
        list.forEach(e -> e.put("num", num.getAndIncrement()));
    }

    /**
     * 构建上一条数据的问题集
     * @param vo 参数对象
     */
//    public static void buildLastMap(ExistProblemVo vo) {
//        Map<String, ProblemParamDTO> lastExistProblemMap = vo.getLastExistProblemMap();
//        if (lastExistProblemMap == null || lastExistProblemMap.size() == 0) {
//            return;
//        }
//        Map<String, ProblemParamDTO> existProblemMap = vo.getExistProblemMap();
//        Map<String, String> defaultValues = vo.getDefaultValues();
//        for (Map.Entry<String, ProblemParamDTO> entry : lastExistProblemMap.entrySet()) {
//            String defaultValue = defaultValues.get(entry.getKey());
//            // 从本次数据中捞取上次的数据
//            ProblemParamDTO p = existProblemMap.get(entry.getKey());
//            if (p == null) {
//                ProblemParamDTO p1 = entry.getValue();
//                p1.setKey(defaultValue).setRectificateType(RectificateTimeEnum.IMMEDIATE_RECTIFICAIOTN.getCode());
//                existProblemMap.put(entry.getKey(), p1);
//            }
//        }
//    }

    /**
     * 构建问题对象
     * @param problemVo
     * @param formSummary
     */
//    public static void buildExistProblemVo(ExistProblemVo problemVo, Sqfw_formSummary_new formSummary) {
//        // 1 获取所有有问题的数据编号
//        getExistProblemMap(problemVo);
//        // 2 获取上一次的数据
//        Sqfw_formSummary_new lastData = queryLastData(formSummary, ServiceTypeEnum.SAFE_CHECK.getName());
//        if (lastData == null) {
//            return;
//        }
//        SafeCheckTaskPdfDTO lastPdfDTO = JSONObject.parseObject(lastData.getParamData(), SafeCheckTaskPdfDTO.class);
//        ExistProblemVo vo = new ExistProblemVo();
//        vo.setDefaultValues(problemVo.getDefaultValues()).setLastExistProblemMap(lastPdfDTO.getExistProblemMap())
//                .setCurrentDataProblemValues(CenturyCitySafeProblemEnum.getProblemValues(lastPdfDTO))
//                .setRiskLevel(lastPdfDTO.getRiskLevel())
//                .setExistProblemMap(problemVo.getExistProblemMap());
//        CenturyCitySecurityComponent.buildLastMap(vo);
//    }

    /**
     * 获取所有有问题的数据编号
     * @return
     */
//    public static void getExistProblemMap(ExistProblemVo vo) {
//        // 存在的问题编号
//        Map<String, ProblemParamDTO> existProblemMap = new HashMap<>();
//        // 问题的默认值
//        Map<String, String> defaultValues = vo.getDefaultValues();
//        // 当前数据的参数
//        Map<String, ProblemParamDTO> problemValues = vo.getCurrentDataProblemValues();
//        for (Map.Entry<String, ProblemParamDTO> entry : problemValues.entrySet()) {
//            String defaultValue = defaultValues.get(entry.getKey());
//            ProblemParamDTO p = entry.getValue();
//            // 前端输入值 但是和默认值不一致 需要记录问题编号
//            if (p != null && !defaultValue.equals(p.getKey())) {
//                p.setHazardLevelType(vo.getRiskLevel());
//                existProblemMap.put(entry.getKey(), p);
//                continue;
//            }
//            p = new ProblemParamDTO();
//            p.setKey(defaultValue);
//            entry.setValue(p);
//            problemValues.put(entry.getKey(), p);
//        }
//        vo.setExistProblemMap(existProblemMap);
//    }
}
